import { createApp, type App as VueApp } from "vue";
import {
    renderWithQiankun,
    qiankunWindow,
    type QiankunProps,
} from "vite-plugin-qiankun/dist/helper";
// @ts-ignore
import App from "./App.vue";

let app: VueApp<Element>;

// 挂载函数
function renderApp(container: any) {
    app = createApp(App);
    app.mount(container);
}

// 是否是主应用打开子应用
if (qiankunWindow.__POWERED_BY_QIANKUN__) {
    renderWithQiankun({ bootstrap, mount, unmount, update });
} else {
    renderApp("#app");
}

// 初始化启动子应用
async function bootstrap() {
    console.log("初始化启动子应用");
}

async function mount(props: QiankunProps) {
    console.log("子应用1挂载，主应用传递参数：", {
        nickName: props.nickName,
        age: props.age
    });
    localStorage.setItem('userInfo', JSON.stringify({
        nickName: props.nickName,
        age: props.age
    }))
    renderApp(props.container?.querySelector("#app"));
}

async function unmount() {
    console.log("子应用卸载");
    app?.unmount();
}

async function update() {
    console.log("子应用更新");
}